﻿using System;
using System.Threading;
using Net.System;

namespace Net.Share
{
    public class ThreadGroup
    {
        public int Id;
        public Thread Thread;
        public int FPS;

        public virtual void Add(object worker)
        {
        }

        public virtual void Remove(object worker)
        {
        }

        public override string ToString()
        {
            return $"线程组ID:{Id} 线程ID:{Thread.ManagedThreadId}";
        }
    }

    public class ThreadGroup<Worker> : ThreadGroup
    {
        public FastListSafe<Worker> Workers = new();

        public override void Add(object worker)
        {
            Workers.Add((Worker)worker);
        }

        public override void Remove(object worker)
        {
            Workers.Remove((Worker)worker);
        }

        public override string ToString()
        {
            return $"线程组ID:{Id} 线程ID:{Thread.ManagedThreadId} 工人数量:{Workers.Count}";
        }
    }

    public class ThreadGroup<Worker1, Worker2> : ThreadGroup
    {
        public FastListSafe<Worker1> Workers1 = new();
        public FastListSafe<Worker2> Workers2 = new();

        public override void Add(object worker)
        {
            if (worker is Worker1 worker1)
            {
                Workers1.Add(worker1);
            }
            else if (worker is Worker2 worker2)
            {
                Workers2.Add(worker2);
            }
        }

        public override void Remove(object worker)
        {
            if (worker is Worker1 worker1)
            {
                Workers1.Remove(worker1);
            }
            else if (worker is Worker2 worker2)
            {
                Workers2.Remove(worker2);
            }
        }

        public override string ToString()
        {
            return $"线程组ID:{Id} 线程ID:{Thread.ManagedThreadId} 工作数量:{Workers1.Count + Workers2.Count}";
        }
    }
}
